package cn.tedu._04weibo.controller;

import cn.tedu._04weibo.base.response.JsonResult;
import cn.tedu._04weibo.base.response.StatusCode;
import cn.tedu._04weibo.mapper.CommentMapper;
import cn.tedu._04weibo.pojo.datatransferobject.CommentAddParam;
import cn.tedu._04weibo.pojo.entity.Comment;
import cn.tedu._04weibo.pojo.entity.Weibo;
import cn.tedu._04weibo.pojo.valueobject.CommentVO;
import cn.tedu._04weibo.pojo.valueobject.UserVO;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpSession;
import lombok.extern.slf4j.Slf4j;
import org.apache.tomcat.Jar;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/v1/comment/")
@Tag(name = "03-微博模块")
@Slf4j
public class CommentController {
    @Autowired
    private CommentMapper commentMapper;

    @PostMapping("insert")
    @Operation(summary = "01-发表评论")
    @ApiOperationSupport(order = 10)
    public JsonResult addComment(@RequestBody CommentAddParam commentAddParam, HttpSession session) {
        log.debug("commentAddParam:" + commentAddParam);
        UserVO userVO = (UserVO) session.getAttribute("user");
//        if (userVO == null) {
//            return new JsonResult(StatusCode.NOT_LOGIN);
//        } else {
            Comment comment = new Comment();
            BeanUtils.copyProperties(commentAddParam, comment);
            comment.setCreateTime(new Date());
            comment.setUserId(userVO.getId());
            comment.setWeiboId(commentAddParam.getWeiboId());
            int num = commentMapper.addco(comment);
            if (num > 0) {
                return JsonResult.Bingo();
            }
            return new JsonResult(StatusCode.OPERATION_FAILED);
//        }
    }

    @GetMapping("selectByWeiboId")
    @Operation(summary = "02-获取评论列表")
    @ApiOperationSupport(order = 20)
    public JsonResult selectById(Long weiboId, HttpSession session) {
        log.debug("weiboId:" + weiboId);
        UserVO userVO = (UserVO) session.getAttribute("user");
//        if (userVO == null) {
//            return new JsonResult(StatusCode.NOT_LOGIN);
//        } else {
            List<CommentVO> commentVOList = commentMapper.queryById(weiboId);
            return JsonResult.Bingo(commentVOList);
        }
//    }
}
